package priv.utrix.micro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Singleton;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import priv.utrix.micro.dao.SysUserDao;
import priv.utrix.micro.dto.SysUserDTO;
import priv.utrix.micro.entity.SysUser;
import priv.utrix.micro.pojo.SysUserDO;
import priv.utrix.micro.service.AuthorityService;
import priv.utrix.micro.service.SysUserService;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author makejava
 * @since 2020-09-28 15:33:30
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {

    final AuthorityService authorityService;
    final SysUserDao userDao;

    @Override
    public SysUserDTO getByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = userDao.selectOne(wrapper);
        if (sysUser == null) {
            return Singleton.get(SysUserDTO.class);
        }

        SysUserDO userDO = authorityService.listAuthorityByUsername(username);
        SysUserDTO userDTO = BeanUtil.copyProperties(sysUser, SysUserDTO.class);
        return userDTO.setRoleIds(userDO.getRoleIds()).setAuthorityCodeList(userDO.getAuthorityCodeList());
    }
}
